from arquivos import criarArquivos,escreveArquivos,fecharArquivos
from CIArtigo1 import r1,r2,r3,p1,p2,p3,H,m1,m2,m3
from numpy.linalg import norm
from numpy import zeros
from F import Fr3,Fp3
from constantes import G
from time import time

tFinal=10
dimensao=2
metodo='artigo1'
dimensao=2
SO='w'

n=13
T=2**n+1
dt=tFinal/(T-1)

f=criarArquivos(tFinal,n,SO,metodo);

f=escreveArquivos(f,r1,r2,r3,p1,p2,p3,H,20,2)
    
c=zeros(4)
d=zeros(4)
t=(2-2**(1/3));
t=1/t;
c[0]=t/2;
c[3]=c[0];
c[1]=t/2;
c[1]=c[1]*(1-2**(1/3));
c[2]=c[1];
d[0]=t;
d[2]=t;
d[1]=t;
d[1]=-d[1]*2**(1/3);
d[3]=0;

a=time()
for i in range(1,T):
    for j in range(4):
        dx=Fr3(p1,p2,p3,m1,m2,m3)
        r1=r1+dt*c[j]*dx[0:2];
        r2=r2+dt*c[j]*dx[2:4];
        r3=r3+dt*c[j]*dx[4:];
       
        dx=Fp3(r1,r2,r3,m1,m2,m3);
        p1=p1+dt*d[j]*dx[0:2];
        p2=p2+dt*d[j]*dx[2:4];
        p3=p3+dt*d[j]*dx[4:];
        
    H=(norm(p1)**2)/(2*m1)+(norm(p2)**2)/(2*m2)+(norm(p3)**2)/(2*m3)-G*m1*m2/norm(r1-r2)-G*m1*m3/norm(r1-r3)-G*m2*m3/norm(r2-r3);
    f=escreveArquivos(f,r1,r2,r3,p1,p2,p3,H,20,2)


fecharArquivos(f);
b=time()
print('%0.3f segundos' % float(b-a))





